package be.kuleuven.mume.hulpklassen;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
import be.kuleuven.mume.R;
import be.kuleuven.mume.framework.Mobilenvi;
import be.kuleuven.mume.framework.Tweet;

public class TwitterHelper {
	private String TAG = "TwitterHelper";
	private State state = State.initial;
	private Context context;
	private Context baseContext;
	private boolean debug = Mobilenvi.debug;
	public boolean showIsverified = false;
	
	public enum State { initial, verifying, verified, atBrowser, sendingTweet, ready}
	
	public TwitterHelper(Context baseContext, Context context){
		this.baseContext = baseContext;
		this.context = context;
	}
	
	public boolean checkTwitterPermissions(final boolean openBrowser, String vakId) {
		if(state.equals(State.verified))
			return true;
		if(state.equals(State.verifying))
			return false;
		else {
			JSONHandler handler = new JSONHandler() {	
				@Override
				public void handelJSON(JSONObject obj) {
					if(obj.has("result")) {
						setState(State.verified);
						if(showIsverified){
			        		Toast toast = Toast.makeText(context, R.string.twitter_verified, 5);
			        		toast.show();
						}
						return;
					}
					else if(openBrowser) {
						String link;
						try {
							link = obj.getString("link");
						    Intent i = new Intent(Intent.ACTION_VIEW);  
						    i.setData(Uri.parse(link));  
						    context.startActivity(i);
						    setState(State.atBrowser);
						} catch (JSONException e) {
							Log.e(TAG, "JSONException", e);
						}
					}
				}
			};
			
			HttpHelper.doGetInThread(Tweet.getSigninUrl(vakId), baseContext, handler);
			setState(State.verifying);
			return false;
		}
	}

	public void showInputDialog() {

		AlertDialog.Builder alert = new AlertDialog.Builder(context);

		alert.setTitle("Tweet");
		alert.setMessage(R.string.twitter_question);

		// Set an EditText view to get user input 
		final EditText input = new EditText(context);
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String value = input.getText().toString();
				JSONObject post = new JSONObject();
				try {
					post.put("tweet", value);
					HttpHelper.doPostInThread(Tweet.ADD_URL, post, baseContext, getResponceHandler());
				} catch (JSONException e) {
					Log.e(TAG, "JSONException", e);
				}
			}
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				// Canceled.
			}
		});

		alert.show();
	}
	
	private JSONHandler getResponceHandler() {
		JSONHandler h = new JSONHandler(){
			@Override
			public void handelJSON(JSONObject obj) {
				if(obj.has("result"))
					return;
				else
					Log.e(TAG, "ServerException" + obj.toString());
			}
		};
		
		return h;
	}
	
	private void setState(State state) {
		this.state = state;
		if(debug)Log.d(TAG, "CurrentState:" + this.state.toString());
	}
	
	public State getState(){
		return state;
	}

	public void returnedFromBrowser() {
		setState(State.initial);
	}
}
